﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

// Step by Step with NIKI's video

struct Player
{
    public int x;
    public int y;
    public string c;
    public ConsoleColor color;
}

class FallingRocks
{
    static void PrintOnPosition(int x, int y, string c, ConsoleColor color = ConsoleColor.Gray)
    {
        Console.SetCursorPosition(x, y);
        Console.ForegroundColor = color;
        Console.Write(c);
    }

    static void PrintStringOnPosition(int x, int y, string str, ConsoleColor color = ConsoleColor.Gray)
    {
        Console.SetCursorPosition(x, y);
        Console.ForegroundColor = color;
        Console.Write(str);
    }

    static void Main()
    {
        Console.BufferHeight = Console.WindowHeight = 20;
        Console.BufferWidth = Console.WindowWidth = 50;
        int playfieldWidth = Console.WindowWidth - 10;
        int livesCount = 10;
        Player dwarf = new Player();
        dwarf.x = playfieldWidth / 2 - 1;
        dwarf.y = Console.WindowHeight - 1;
        dwarf.c = "(0)";
        dwarf.color = ConsoleColor.White;
        Random r = new Random();
        List<Player> rocks = new List<Player>();
        Random randomGenerator = new Random();

        while (true)
        {
            // print dwarf
            PrintOnPosition(dwarf.x, dwarf.y, dwarf.c, dwarf.color);

            // move dwarf (me) (key pressed)
            while (Console.KeyAvailable)
            {
                ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                if (pressedKey.Key == ConsoleKey.LeftArrow)
                {
                    if (dwarf.x - 1 >= 0)
                    {
                        dwarf.x = dwarf.x - 1;
                    }
                }
                else if (pressedKey.Key == ConsoleKey.RightArrow)
                {
                    if (dwarf.x + 1 < playfieldWidth - 2)
                    {
                        dwarf.x = dwarf.x + 1;
                    }
                }
            }

            int chance = randomGenerator.Next(0, 100);
            if (chance < 10)
            {
                Player newRock = new Player();
                newRock.color = (ConsoleColor)randomGenerator.Next(15);
                newRock.c = "---";
                newRock.x = randomGenerator.Next(0, playfieldWidth);
                newRock.y = 0;
                rocks.Add(newRock);
            }
            else if (chance < 20)
            {
                Player newRock = new Player();
                newRock.color = (ConsoleColor)randomGenerator.Next(15);
                newRock.c = "##";
                newRock.x = randomGenerator.Next(0, playfieldWidth);
                newRock.y = 0;
                rocks.Add(newRock);
            }

            else if (chance < 30)
            {
                Player newRock = new Player();
                newRock.color = (ConsoleColor)randomGenerator.Next(15);
                newRock.c = "o";
                newRock.x = randomGenerator.Next(0, playfieldWidth);
                newRock.y = 0;
                rocks.Add(newRock);
            }

            // move rocks
            List<Player> newList = new List<Player>();
            for (int i = 0; i < rocks.Count; i++)
            {
                Player oldRock = rocks[i];
                Player newRock = new Player();
                newRock.x = oldRock.x;
                newRock.y = oldRock.y + 1;
                newRock.c = oldRock.c;
                newRock.color = oldRock.color;

            // check if some of the rocks hits the dwarf 

                if (newRock.y == dwarf.y && ((newRock.x) == dwarf.x | (newRock.x + 1) == dwarf.x | (newRock.x+2) == dwarf.x))
                {
                    livesCount--;
                }
                else if (newRock.y < Console.WindowHeight)
                {
                    newList.Add(newRock);
                }
            }
            rocks = newList;

            // clear playfield
            Console.Clear();
            
            // redraw playfield
            // print dwarf
            PrintOnPosition(dwarf.x, dwarf.y, dwarf.c, dwarf.color);
            
            // print rocks    
            foreach (Player rock in rocks) // the rocks list is the collection
            {
                PrintOnPosition(rock.x, rock.y, rock.c, rock.color);
            }

            // Draw info

            PrintStringOnPosition((playfieldWidth + 1), 1, "Lives: " + livesCount, ConsoleColor.White);

            Thread.Sleep(80);
        }
    }
}